package com.example.rbacmanage.util;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;

public class DateUtils {
    public static String getStartDate(){
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 一天的开始时间（当天00:00:00）
        LocalDateTime startOfDay = today.atStartOfDay();
        // 定义日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return startOfDay.format(formatter);
    }

    public static String getEndDate(){
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 一天的结束时间（次日00:00:00的前一秒）
        LocalDateTime endOfDay = today.plusDays(1).atStartOfDay().minusNanos(1);
        // 定义日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return endOfDay.format(formatter);
    }

    public static String getBeforeDate(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -days);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(calendar.getTime());
    }

    public static String getStartDate(String dateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate today = LocalDate.parse(dateStr, formatter);
        LocalDateTime startOfDay = today.atStartOfDay();
        return startOfDay.format(formatter);
    }

    public static String getEndDate(String dateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDate today = LocalDate.parse(dateStr, formatter);
        LocalDateTime endOfDay = today.plusDays(1).atStartOfDay().minusNanos(1);
        return endOfDay.format(formatter);
    }
}
